package com.wrs.androidkeepalive.service;

import android.accessibilityservice.AccessibilityService;
import android.content.Intent;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;


import com.wrs.androidkeepalive.utils.NotificationUtils;
import com.wrs.androidkeepalive.utils.TaskUtils;

import java.util.Timer;
import java.util.TimerTask;
// 开启无障碍会回调：onCreate -》  onServiceConnected -》 onAccessibilityEvent。。。
// 关闭无障碍会回调：onDestroy
// 启动服务会回调：onStartCommand
public class MyAccessibilityService extends AccessibilityService {
    String tag = "保活 MyAccessibilityService";
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {

        @Override

        public void run() {

// 执行某个任务



        }

    };
//    Runnable task = new Runnable() {
//
//        @Override
//
//        public void run() {
//
//// 执行某个任务
//
//
//
//        }
//
//    };


    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(tag, "onCreate");
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Log.e(tag, "onAccessibilityEvent");

    }

    @Override
    public void onInterrupt() {
        // 中断无障碍服务
        Log.e(tag, "onInterrupt");
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
//        TaskUtils.startTask(tag, getApplicationContext(), this);
//        service.scheduleAtFixedRate(task, 5, 10, TimeUnit.SECONDS);
//        timer.schedule(task, 5000, 10000);
        Log.e(tag, "onStartCommand");
        NotificationUtils.startNot(this, this);
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.e(tag, "onServiceConnected");
        TaskUtils.startTask(tag, getApplicationContext(), this);
        NotificationUtils.startNot(this, this);
//
//        AccessibilityServiceInfo serviceInfo = new AccessibilityServiceInfo();
//        serviceInfo.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
//        serviceInfo.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
//        serviceInfo.packageNames = new String[]{"com.tencent.mm"};
//        serviceInfo.notificationTimeout=100;
//        setServiceInfo(serviceInfo);

        // 无障碍服务已连接, 服务启动时
        // 执行一些初始化操作，例如注册广播接收器
//        IntentFilter filter = new IntentFilter();
//        filter.addAction("android.intent.action.MY_KEEP_ALIVE");
//        registerReceiver(keepAliveReceiver, filter);


//        startTask();


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 执行一些清理操作，例如取消注册广播接收器
        Log.e(tag, "onDestroy");
//        stopForeground(true);
    }



}
